package com.yonder.test.shutdown;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;

public class ServerHandler extends SimpleChannelInboundHandler<String> {
	
    @Override
    //客户端连接
    public void channelActive(final ChannelHandlerContext ctx) {
    	System.err.println("有新客户端连接");
    }

    @Override
    public void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
    	System.err.println("msg"+msg);
    }
    
    @Override
    // 客户端关闭
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
    	System.err.println("channelInactive");
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        cause.printStackTrace();
        ctx.close();
    }

	@Override
	public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
		 if (evt instanceof IdleStateEvent){
	        IdleStateEvent event = (IdleStateEvent)evt;
	        if (event.state()== IdleState.WRITER_IDLE){
	        	System.out.println("write idle");
	        } else if (event.state()== IdleState.READER_IDLE){
	        	System.out.println("read idle");
	        	ctx.close();
	        }
	    }else {
	    	super.userEventTriggered(ctx, evt);
	    }
	}
}

